#include "pathshowwid.h"


#define FINISH ("background-color: rgb(255, 255, 0)")
#define EXEC ("background-color: rgb(85, 255, 0)")
#define UNEXEC ("background-color: rgb(200, 191, 195);")

PathShowWId::PathShowWId(QWidget* parent)
	: QWidget(parent)
{
	current = 0;
	ui.setupUi(this);
}

PathShowWId::~PathShowWId()
{}

void PathShowWId::ShowPath(QString& pathname, const ExecPathList& info)
{
	if (m_name == pathname) {
		if (info.currpoint != current % 10) {
			current = info.currpoint % 10;
			CurrentChanged();
		}
		return;
	}
	ClearPath();
	m_name = pathname;
	int x = 0, y = 0;
	for (auto& iter : info.paths) {
		QLabel* point = new QLabel(iter);
		point->setFixedSize(80, 25);
		m_path.append(point);
		//QLabel* temp = new QLabel("->");
		ui.gridLayout_2->addWidget(point, x / 3, y % 3);
		//ui.gridLayout_2->addWidget(temp, x / 8, y % 8 + 1);
		x += 1;
		y += 1;
	}
	current = info.currpoint % 10;
	CurrentChanged();
}

void PathShowWId::CurrentChanged()
{
	for (int i = 0; i < current && i < m_path.size(); i++) {
		m_path.at(i)->setStyleSheet(FINISH);
	}
	if (current < m_path.size()) {
		m_path.at(current)->setStyleSheet(EXEC);
	}
	for (int i = current + 1; i < m_path.size(); i++) {
		m_path.at(i)->setStyleSheet(UNEXEC);
	}
}

void PathShowWId::ClearPath()
{

	for (auto& iter : m_label) {
		ui.gridLayout_2->removeWidget(iter);
		iter->deleteLater();
	}
	m_label.clear();

	for (auto& iter : m_path) {
		ui.gridLayout_2->removeWidget(iter);
		iter->deleteLater();
	}

	m_path.clear();
}
